import { localStg } from '@/utils/storage';
const LOCAL_NAME = 'productStore';

export interface State {
  activeMenu: number;
  dict: any;
  uploadFiles: any[];
  isGetDraftData: boolean;
  canvasConfig: {
    size: string;
    direction: string;
    icon: string;
    width: number;
    height: number;
  };
  canvas: any[];
  voice: any;
  material: any;
  bgTab: number;
  bg: any;
  defaultBg: any; // 默认背景
  digital: any;
  defaultDigital: any; // 默认数字人
  text: string;
  videoName: string;
  cover: string;
  updateFlag: boolean;
  dictPromise: Promise<any> | null;
}

export function defaultState(): State {
  return JSON.parse(
    JSON.stringify({
      activeMenu: 0, // 当前激活侧边栏
      isGetDraftData: false, // 是否获取草稿数据
      // 字典表
      dict: {
        canvas: [
          {
            size: '9:16',
            label: '竖屏 9:16',
            direction: 'vertical',
            resolution: {
              width: 1080,
              height: 1920
            },
            icon: 'product-screen_9'
          },
          {
            size: '3:4',
            label: '竖屏 3:4',
            direction: 'vertical',
            resolution: {
              width: 1080,
              height: 1440
            },
            icon: 'product-screen_3'
          },
          {
            size: '16:9',
            label: '横屏 16:9',
            direction: 'horizontal',
            resolution: {
              width: 1920,
              height: 1080
            },
            icon: 'product-screen_16'
          },
          {
            size: '4:3',
            label: '横屏 4:3',
            direction: 'horizontal',
            resolution: {
              width: 1440,
              height: 1080
            },
            icon: 'product-screen_4'
          }
        ]
      },
      uploadFiles: [], // 上传文件
      // 默认画布配置
      canvasConfig: {
        size: '9:16',
        direction: 'vertical',
        icon: 'product-screen_9',
        width: 1080,
        height: 1920
      },
      canvas: [], // 画布导出内容
      voice: {}, // 选中声音
      material: {},
      bgTab: 1,
      bg: {},
      defaultBg: {}, // 默认背景
      digital: {},
      defaultDigital: {}, // 默认数字人
      text: '', // 播报文本
      videoName: '', // 任务名称
      cover: '', // 封面
      dictPromise: null, // 字典表Promise
      updateFlag: false // 更新标识
    })
  );
}

export function getLocalState(): State {
  // const localSetting: State | undefined = localStg.get(LOCAL_NAME);
  return {
    ...defaultState()
  };
}

export function setLocalState(setting: State): void {
  localStg.set(LOCAL_NAME, setting);
}

export function removeLocalState() {
  localStg.remove(LOCAL_NAME);
}
